package com.jp.study.Interceptor;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
* @author dinghao
* @ClassName: MethodConsumedTimeRecorder 
* @Description: 方法耗时日志记录器，用于记录一个方法前后耗时，并以info级别打印出日志
* @date 2018年3月21日 下午5:21:02 
*
 */
public class MethodConsumedTimeRecorder implements MethodInterceptor{
	
	private final Logger logger=LoggerFactory.getLogger(getClass());
	
	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		long startTime=System.currentTimeMillis();
		Object result=invocation.proceed();
		long endTime=System.currentTimeMillis();
		Method method = invocation.getMethod();
		String declaredClassName=method.getDeclaringClass().getName();
		String methodName=method.getName();
		logger.info("{}类{}方法成功执行，耗时{}毫秒",declaredClassName,methodName,endTime-startTime);
		return result;
	}
}
